package com.platform.wx.mp.controller;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.MpMsgEntity;
import com.platform.service.MpMsgService;
import com.platform.util.WxMpArticleUtil;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * Controller
 *
 * @author LarkerJin
 * @email 277901879@qq.com
 * @date 2019-07-03 15:32:04
 */
@RestController
@RequestMapping("mpmsg")
public class MpMsgController {
    @Autowired
    private MpMsgService mpMsgService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mpmsg:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MpMsgEntity> mpMsgList = mpMsgService.queryList(query);
        int total = mpMsgService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(mpMsgList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mpmsg:info")
    public R info(@PathVariable("id") Integer id) {
        MpMsgEntity mpMsg = mpMsgService.queryObject(id);
        mpMsg.setAesKey("XXXXXXXXXX");
        mpMsg.setMpSecret("XXXXXXXXXX");
        return R.ok().put("mpMsg", mpMsg);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mpmsg:save")
    public R save(@RequestBody MpMsgEntity mpMsg) {
        mpMsgService.save(mpMsg);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mpmsg:update")
    public R update(@RequestBody MpMsgEntity mpMsg) {
        mpMsgService.update(mpMsg);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mpmsg:delete")
    public R delete(@RequestBody Integer[] ids) {
        mpMsgService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<MpMsgEntity> list = mpMsgService.queryList(params);
        return R.ok().put("list", list);
    }
    
    /**
     * 抓取微信文章
     */
    @RequestMapping("/article")
    public R article(HttpServletRequest request) {
    	String url = request.getParameter("url");
    	if(url == null)
    		return R.error("微信地址不能为空");
    	try {
    		url = URLDecoder.decode(url,"UTF-8").replaceAll("&amp;", "&");
        	Map<String,String> article = WxMpArticleUtil.fetchArticleByWx(url,true);
            return R.ok().put("article", article);
    	}catch(Exception e) {
    		return R.error(e.getMessage());
    	}
    }

}
